﻿package ywh.model
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import ywh.event.CoreEvent;
	import ywh.event.StageVideoMouseEvent;
	import ywh.manager.Core;
	import ywh.manager.CoreState;
	import ywh.manager.ICourseDataProvider;
	import ywh.manager.Sync;
	import ywh.player.RelayoutSprite;
	import ywh.pool.PlayerPool;
	
	/**
	 *
	 * @author 杨伟华
	 */
	public class Model2 extends Sprite
	{
		public var builderInfo:MovieClip;
		public var listPanel:MovieClip;
		public var thinkPanel:MovieClip;
		public var thinkTextPanel:Model2TextPanel;
		
		public var videoCtrl:Model2PlayCtrl;
		public var titlePanel:Model2TitlePanel;
		public var picsCtrl:Model2PicsCtrl;
		public var background:Model2BackGround;
		
		private var _otherInfoProvider:Object;
		
		private var _currentLayout:uint = 0;
		
		public var picsLayout:RelayoutSprite;
		public var videoLayout:RelayoutSprite;
		private var hasInit:Boolean = false;
		
		/**
		 * 构建一个课程播放组件
		 * @param	dp 课程播放组件容器，需要具备以下特点：1.stage不为空;2.实现了ICourseDataProvider;
		 * @return
		 */
		public static function createModel(dp:DisplayObjectContainer):Model2
		{
			RelayoutSprite.stageRef = dp.stage;
			var model2:Model2 = new Model2(dp);
			return model2;
		}
		
		public function Model2(oip:Object)
		{
			_otherInfoProvider = oip;
			Core.rendersContainer = this;
			Core.rendersContainer = this;
			Core.resolvePathHandle = oip["getResolvePath"];
			Core.addEventListener(CoreEvent.CORE_STATE_CHANGE, onCoreStreamReady);
		}
		
		public function changeChapterHandle():void
		{
			background.title.load(new URLRequest(otherInfoProvider["getResolvePath"]("datas/title2.png")));
			Core.state = CoreState.EMPTY;
		}
		 
		private function onCoreStreamReady(e:CoreEvent):void
		{
			if (Core.state == CoreState.STREAMS_READY)
			{
				//Core.removeEventListener(CoreEvent.CORE_STATE_CHANGE, onCoreStreamReady);
				hasInit || startSync();
				var bookmarkTime:Number = otherInfoProvider["bookmarkTime"];
				Sync.time = (bookmarkTime > 0 ? bookmarkTime : 0);
				otherInfoProvider["bookmarkTime"] = NaN;
				Sync.pause();
			}
		}
		
		private function startSync():void
		{
			hasInit = true;
			initShow();
			initEvent();
			//子模块;
			background.startSync();
			videoCtrl.startSync();
			picsCtrl.startSync();
			titlePanel.startSync();
			addEventListener(Event.REMOVED_FROM_STAGE, toStageToggleVideo);
			addEventListener(Event.ADDED_TO_STAGE, toStageToggleVideo);
			if (parent)
			{
				parent.removeChild(this);
			}
			videoLayout.bandVideoVisible = false;
		}
		
		private function initShow():void
		{
			removeChild(builderInfo);
			removeChild(listPanel);
			removeChild(thinkPanel);
			//联想面板;
			thinkTextPanel = new Model2TextPanel();
			thinkTextPanel.setSize(thinkPanel.width - 5, thinkPanel.height - 40, 0);
			thinkTextPanel.x = 10;
			thinkTextPanel.y = 0;
			thinkPanel.addChild(thinkTextPanel);
			toggleLayout(0);
		}
		
		private function initEvent()
		{
			builderInfo.addEventListener(Event.ADDED_TO_STAGE, handlePanelAddToStage);
			thinkPanel.addEventListener(Event.ADDED_TO_STAGE, handlePanelAddToStage);
			listPanel.addEventListener(Event.ADDED_TO_STAGE, handlePanelAddToStage);
			Core.addEventListener(StageVideoMouseEvent.MOUSE_DOWN, stageVideoMouseDown);
			PlayerPool.getPlayerById("picsshow").render.addEventListener(MouseEvent.MOUSE_DOWN, handlePicsShowerMouseDown);
		}
		
		private function toStageToggleVideo(e:Event):void
		{
			if (e.type == Event.ADDED_TO_STAGE)
			{
				videoLayout.bandVideoVisible = true;
				Sync.play();
			}
			else
			{
				Sync.pause();
				videoLayout.bandVideoVisible = false;
			}
		}
		
		private function handlePanelAddToStage(e:Event):void
		{
			switch (e.target)
			{
				case thinkPanel: 
					if (listPanel.parent)
					{
						removeChild(listPanel);
					}
					background.thinkBtn.gotoAndStop("on");
					thinkTextPanel.setText(otherInfoProvider["questionInfo"], null);
					thinkPanel.addEventListener(MouseEvent.MOUSE_DOWN, Panel_mouseDown);
					break;
				case listPanel: 
					if (thinkPanel.parent)
					{
						removeChild(thinkPanel);
					}
					background.listBtn.gotoAndStop("on");
					listPanel.addEventListener(MouseEvent.MOUSE_DOWN, Panel_mouseDown);
					break;
			}
			videoLayout.bandVideoVisible = false;
			stage.addEventListener(MouseEvent.MOUSE_DOWN, stage_mouseDown);
			e.target.addEventListener(Event.REMOVED_FROM_STAGE, handlePanelRemoveFromStage);
		
		}
		
		private function handlePanelRemoveFromStage(e:Event):void
		{
			switch (e.target)
			{
				case thinkPanel: 
					background.thinkBtn.gotoAndStop("off");
					thinkPanel.removeEventListener(MouseEvent.MOUSE_DOWN, Panel_mouseDown);
					thinkPanel.removeEventListener(Event.REMOVED_FROM_STAGE, handlePanelRemoveFromStage);
					break;
				case listPanel: 
					background.listBtn.gotoAndStop("off");
					listPanel.removeEventListener(MouseEvent.MOUSE_DOWN, Panel_mouseDown);
					listPanel.removeEventListener(Event.REMOVED_FROM_STAGE, handlePanelRemoveFromStage);
					break;
			}
			videoLayout.bandVideoVisible = true;
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, stage_mouseDown);
		}
		
		private function Panel_mouseDown(e:MouseEvent):void
		{
			e.stopImmediatePropagation();
		}
		
		private function stage_mouseDown(e:MouseEvent):void
		{
			if (builderInfo.parent)
			{
				removeChild(builderInfo);
			}
			if (thinkPanel.parent)
			{
				removeChild(thinkPanel);
			}
			if (listPanel.parent)
			{
				removeChild(listPanel);
			}
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, stage_mouseDown);
		}
		
		internal function toggleLayout(layout:int = -1)
		{
			if (layout < 0)
			{
				_currentLayout = (_currentLayout + 1) % 3; //循环切换;
			}
			else
			{
				_currentLayout = layout;
			}
			titlePanel.cnBtn.visible = true;
			titlePanel.enBtn.visible = true;
			titlePanel.bg.visible = true;
			videoCtrl.bg.visible = true;
			picsLayout.bandRender.visible = true;
			videoLayout.bandVideoVisible = true;
			background.visible = false;
			picsCtrl.visible = false;
			switch (_currentLayout)
			{
				case 0: 
					background.visible = true;
					picsCtrl.visible = true;
					titlePanel.bg.visible = false;
					videoCtrl.bg.visible = false;
					titlePanel.setRectangle();
					videoCtrl.setRectangle();
					picsLayout.setRectangle();
					videoLayout.setRectangle();
					break;
				case 1: 
					titlePanel.cnBtn.visible = false;
					titlePanel.enBtn.visible = false;
					picsLayout.bandRender.visible = false;
					titlePanel.setRectangle(new Rectangle(0, 0, stage.stageWidth, 40));
					videoCtrl.setRectangle(new Rectangle(0, stage.stageHeight - 40, stage.stageWidth, 40));
					videoLayout.setRectangle(new Rectangle(0, 40, stage.stageWidth, stage.stageHeight - 80));
					break;
				case 2: 
					videoLayout.bandVideoVisible = false;
					titlePanel.setRectangle(new Rectangle(0, 0, stage.stageWidth, 40));
					videoCtrl.setRectangle(new Rectangle(0, stage.stageHeight - 40, stage.stageWidth, 40));
					picsLayout.setRectangle(new Rectangle(0, 40, stage.stageWidth, stage.stageHeight - 80));
					break;
			}
		}
		
		private function handlePicsShowerMouseDown(e:MouseEvent):void
		{
			toggleLayout((_currentLayout == 0) ? 2 : 0);
		}
		
		private function stageVideoMouseDown(e:StageVideoMouseEvent):void
		{
			toggleLayout((_currentLayout == 0) ? 1 : 0);
		}
		
		public function get otherInfoProvider():Object
		{
			return _otherInfoProvider;
		}
	
	}
}